package cn.edu.ctbu.simplescoremanagementsystem.service.impl;

import cn.edu.ctbu.simplescoremanagementsystem.domain.SC;
import cn.edu.ctbu.simplescoremanagementsystem.repository.SCRepository;
import cn.edu.ctbu.simplescoremanagementsystem.service.SCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class SCServiceImpl implements SCService {
    @Autowired
    public SCRepository scRepository;

    public SC findByID(Integer id) {
        return scRepository.findById(id).orElse(null);
    }

    public List<SC> findAll() {
        return scRepository.findAll();
    }

    public SC insert(SC sc) {
        return scRepository.save(sc);
    }

    public SC update(SC sc) {
        return scRepository.save(sc);
    }

    public SC Delete(Integer id) throws Exception {
        SC sc = scRepository.findById(id).orElse(null);
        if (sc == null) {
            log.error("删除记录失败，该记录不存在");
            throw new Exception("要删除的记录不存在");
        }
        scRepository.deleteById(id);
        log.info("删除记录成功");
        return sc;
    }

    public Page<SC> getByPage(Pageable pageable){
        return scRepository.findAll(pageable);
    }

    public Page<SC> getByPage(Example<SC> scExample, Pageable pageable){
        return scRepository.findAll(pageable);
    }
}
